<template>
    <div  class="login-container" :style ="note" >
        <center>
            <el-form :model="userData" status-icon :rules="rules" ref="ruleForm" label-width="aoto" class="login-form">
                <el-form-item label="用户名" prop="loginname">
                    <el-input  v-model="userData.loginname" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="userpwd">
                    <el-input type="password" v-model="userData.userpwd" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="">重置</el-button>
                </el-form-item>
            </el-form>
        </center>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                show: false,
                userData: {
                    loginname: '',
                    userpwd: ''
                },
                note: {
                    backgroundImage:'url('+require('@/assets/login.jpg')+')',
                    backgroundRepeat: "no-repeat",
                    backgroundSize: "100% 100%",
                },
                rules: {
                    loginname: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
                    ],
                    userpwd: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
                    ]
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios({
                            method: 'get',
                            url: 'clms-system/user/login',
                            params: {
                                loginname: this.userData.loginname,
                                userpwd: this.userData.userpwd
                            }
                        }).then( result => {
                            if (result.data.code!=200) {
                                this.$message.error(result.data.message);
                            } else {
                                this.$message.success(result.data.message);
                                // 登录成功之后，将 后端生成的 token 存入到 session 中
                                sessionStorage.setItem("token",result.data.token);
                                this.$router.push("/homePage");
                            }
                            this.userData = {}
                        })
                    } else {
                        this.$message.error("输入有误");
                        return false;
                    }
                })
            }
        }
    }
</script>

<style >
    .login-form {
        width: 300px;
        margin: 180px auto;
        background-color: rgba(255, 255, 255, 0.6);
        padding: 50px;
        border-radius: 10px;
        box-shadow: black 6px 6px 50px, black -6px -6px 50px;
        font-weight: bolder;
    }
    /* 背景 */
    .login-container {
        width: 100%;
        height: 100%;
        position: fixed;
    }
</style>
